//引入 koa模块
var Koa = require('koa');
const Router = require('koa-router')
var router = new Router();  /*引入是实例化路由** 推荐*/
const bodyparser = require("koa-bodyparser")
const cors = require("koa2-cors");
//实例化
var app = new Koa();
const data = {
    list: [
        {
            id: 1,
            name: "zzz",
            title: "张正泽",
            collect: true,
            line: false,
            people: "超级管理员",
            createTime: "2007-1-30",


        }, {

            id: 2,
            name: "ldy",
            title: "李东奕",
            collect: false,
            line: true,
            people: "超级测试",
            createTime: "2006-3-3",
        },
        {
            id: 14,
            name: "wqe",
            title: "张正泽",
            collect: true,
            line: false,
            people: "超级管理员",
            createTime: "2007-1-30",


        }, {

            id: 24,
            name: "ldy",
            title: "李东奕q",
            collect: false,
            line: true,
            people: "超级测试",
            createTime: "2006-3-3",
        }
        , {
            id: 121,
            name: "zqzzqwe",
            title: "q张正泽",
            collect: true,
            line: false,
            people: "超级管理员",
            createTime: "2007-1-30",


        }, {

            id: 32,
            name: "ldwwy",
            title: "李东奕w",
            collect: false,
            line: true,
            people: "超级测试",
            createTime: "2006-3-3",
        }
        , {
            id: 21,
            name: "zzzw",
            title: "张正泽w",
            collect: true,
            line: false,
            people: "超级管理员",
            createTime: "2007-1-30",


        }, {

            id: 22,
            name: "ldty",
            title: "李东奕t",
            collect: false,
            line: true,
            people: "超级测试",
            createTime: "2006-3-3",
        },
        {
            id: 11,
            name: "zzzt",
            title: "张正泽t",
            collect: true,
            line: false,
            people: "超级管理员",
            createTime: "2007-1-30",


        }, {

            id: 12,
            name: "ldyt",
            title: "李东奕t",
            collect: false,
            line: true,
            people: "超级测试",
            createTime: "2006-3-3",
        }
    ],
    users: [
        {
            username: "admin",
            password: "123456"
        }
    ]
}
//登录路由
router.post('/data/login', async (ctx) => {
    let { username, password } = ctx.request.body
    let user = data.users.find(item => item.username == username && item.password == password)
    if (user) {
        ctx.body = {
            code: 200,
            message: "登录成功"
        }
    } else if (!user) {
        ctx.body = {
            code: 404,
            message: "未找到账户"
        }
    }

})

router.post('/data', async (ctx) => {
    let obj = ctx.request.body
    data.list.unshift(obj)
    ctx.body = {
        code: 200,
        message: "添加成功"
    }
})

router.put('/data', async (ctx) => {
    let obj = ctx.request.body
    let index = data.list.findIndex(item => item.id == obj.id)

    if (index !== -1) {
        data.list[index] = obj
        ctx.body = {
            code: 200,
            message: "添加成功"
        }
    } else {
        ctx.body = {
            code: 404,
            message: "未找到要修改的数据"
        }
    }
})


router.delete('/data/:id', async (ctx) => {
    let id = ctx.params.id
    let index = data.list.findIndex(item => item.id == id)
    if (index !== -1) {
        data.list.splice(index, 1)
        ctx.body = {
            code: 200,
            message: "删除成功"
        }
    } else {
        ctx.body = {
            code: 404,
            message: "未找到要删除的数据"
        }
    }
})
//分页数据
router.post('/data/emps', async (ctx) => {
    let { page = 1, size = 2, ...searchParams } = ctx.request.body;
    const offset = (page - 1) * size;
    const likeName = searchParams.like_name || ''
    const likeTitle = searchParams.like_title || ''
    const likeCollect = searchParams.like_collect
    const likeLine = searchParams.like_line
    const likePeople = searchParams.like_people || ''
    const likeCreateTime = searchParams.like_createTime || ''
    let filteredList = [...data.list]
    if (likeName) {
        filteredList = filteredList.filter(item => item.name && item.name.toLowerCase().includes(likeName.toLowerCase()))
    }
    if (likeTitle) {
        filteredList = filteredList.filter(item => item.title && item.title.toLowerCase().includes(likeTitle.toLowerCase()))
    }
    if (likePeople) {
        filteredList = filteredList.filter(item => item.people && item.people.toLowerCase().includes(likePeople.toLowerCase()))
    }
    if (likeCreateTime) {
        filteredList = filteredList.filter(item => item.createTime && item.createTime.toLowerCase().includes(likeCreateTime.toLowerCase()))
    }
    if (likeCollect) {
        filteredList = filteredList.filter(item => item.collect !== null && item.collect.toString() == likeCollect)
    }
    if (likeLine) {
        filteredList = filteredList.filter(item => item.line !== null && item.line.toString() == likeLine)
    }

    ctx.body = {
        code: 200,
        data: {
            list: filteredList.slice(offset, offset + size),
            total: filteredList.length,
            page,
            size
        }
    };
});

// router.get('/data', async (ctx) => {
//     const likeName = ctx.query.like_name || ''
//     const likeTitle = ctx.query.like_title || ''
//     const likeCollect = ctx.query.like_collect
//     const likeLine = ctx.query.like_line
//     const likePeople = ctx.query.like_people || ''
//     const likeCreateTime = ctx.query.like_createTime || ''
//     let newlist = [...data.list]
//     if (likeName) {
//         newlist = newlist.filter(item => item.name && item.name.toLowerCase().includes(likeName.toLowerCase()))
//     }
//     if (likeTitle) {
//         newlist = newlist.filter(item => item.title && item.title.toLowerCase().includes(likeTitle.toLowerCase()))
//     }
//     if (likePeople) {
//         newlist = newlist.filter(item => item.people && item.people.toLowerCase().includes(likePeople.toLowerCase()))
//     }
//     if (likeCreateTime) {
//         newlist = newlist.filter(item => item.createTime && item.createTime.toLowerCase().includes(likeCreateTime.toLowerCase()))
//     }
//     if (likeCollect) {
//         newlist = newlist.filter(item => item.collect !== null && item.collect.toString() == likeCollect)
//     }
//     if (likeLine) {
//         newlist = newlist.filter(item => item.line !== null && item.line.toString() == likeLine)
//     }
//     ctx.body = {
//         code: 200,
//         data: newlist
//     }
// })
app.use(cors())
app.use(bodyparser())
app.use(router.routes());   /*启动路由*/
app.use(router.allowedMethods());

app.listen(3000, () => {
    console.log("3000");

});